package com.example.fw;

import java.util.List;
import java.util.Random;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import com.example.tests.GroupData;
import com.example.utils.SortedListOf;

public class GroupHelper extends WebDriverHelperBase {

	public GroupHelper(ApplicationManager manager) {
		super(manager);
	}

	public GroupHelper createGroup(GroupData group) {
    manager.navigateTo().groupsPage();
		initGroupCreation();
		fillGroupForm(group, OVERWRITE);
		submitGroupCreation();
		returnToGroupsPage();
		// update model
		manager.getModelGroups().addGroup(group);
//		rebuildCachedGroups();
		return this;
	}

	public GroupHelper modifyGroup(int index, GroupData group, boolean mode) {
    manager.navigateTo().groupsPage();
		initGroupModification(index);
		fillGroupForm(group, mode);
		submitGroupModification();
		returnToGroupsPage();
		
		if (mode == KEEP) {
		 	// update oldGroup with non-empty fields of group
	    GroupData oldGroup = manager.getModelGroups().getGroups().get(index);
	    oldGroup.updateGroup(group);
			manager.getModelGroups().removeGroup(index).addGroup(oldGroup);
		} else {
			manager.getModelGroups().removeGroup(index).addGroup(group);
		}
		return this;
	}

	public GroupHelper deleteGroup(int index) {
    manager.navigateTo().groupsPage();
		selectGroupByIndex(index);
		submitGroupRemoval();
		returnToGroupsPage();
		manager.getModelGroups().removeGroup(index);
//		rebuildCachedGroups();
		return this;
	}

	//-------------------Low level methods separator--------------------
	
	public SortedListOf<GroupData> getUiGroups() {
		SortedListOf<GroupData> uiGroups = new SortedListOf<GroupData>();

    manager.navigateTo().groupsPage();
		List<WebElement> checkboxes = driver.findElements(By.name("selected[]"));
		for (WebElement checkbox : checkboxes) {
			String title = checkbox.getAttribute("title");
			String grName = title.substring("Select (".length(), title.length() - ")".length());
			uiGroups.add(new GroupData().withName(grName));
		}
		return uiGroups;
	}

	// returns random index if list is not empty, otherwise returns -1
	public int getRandomGroupIndex(SortedListOf<GroupData> groupList) {
    int index = 0;
    if (groupList.size() > 1){
			Random rnd = new Random();
			index = rnd.nextInt(groupList.size()-1);
    } else if (groupList.size() == 0) {
			index = -1;
		}
		return index;
	}

	public GroupHelper initGroupCreation() {
		click(By.name("new"));
		return this;
	}

	public GroupHelper submitGroupCreation() {
		click(By.name("submit"));
		return this;
	}

	public GroupHelper submitGroupRemoval() {
		click(By.name("delete"));
		return this;
	}

	public GroupHelper returnToGroupsPage() {
		click(By.linkText("group page"));
		return this;
	}

	public GroupHelper fillGroupForm(GroupData group, boolean mode) {
		type(By.name("group_name"), group.getGrName(), mode);
		type(By.name("group_header"), group.getGrHeader(), mode);
		type(By.name("group_footer"), group.getGrFooter(), mode);
		return this;
	}

	private void selectGroupByIndex(int index) {
		click(By.xpath("//input[@name='selected[]'][" + (index + 1) +"]"));
	}

	public GroupHelper initGroupModification(int index) {
		selectGroupByIndex(index);
		click(By.name("edit"));
		return this;
	}

	public GroupHelper submitGroupModification() {
		click(By.name("update"));
		return this;
	}

}
